package com.bjpowernode.money.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.bjpowernode.money.constant.IndexConstant;
import com.bjpowernode.money.model.LoanInfo;
import com.bjpowernode.money.service.BidInfoService;
import com.bjpowernode.money.service.LoanInfoService;
import com.bjpowernode.money.service.UserService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;

/**
 * @author ZHAOZHENYU
 * @since 2022/10/14
 */

@Controller
public class IndexController {

    @Reference(interfaceClass = LoanInfoService.class, version = "1.0.0", timeout = 20000)
    private LoanInfoService loanInfoService;

    @Reference(interfaceClass = BidInfoService.class, version = "1.0.0", timeout = 20000)
    private BidInfoService bidInfoService;

    @Reference(interfaceClass = UserService.class,version = "1.0.0",timeout = 20000)
    private UserService userService;

    /**
     * 1、首页年化利率、投资总额、用户数
     * @param model
     * @return
     */
    @GetMapping("/index")
    public String index(Model model, HttpServletRequest request){

        // 后端开启session解决前端静态页面拿不到session的问题
//        request.getSession();

        // 历史年化收益率
        Double loanRateAvg = loanInfoService.queryLoanRateAvg();
        model.addAttribute(IndexConstant.LOAN_RATE_AVG,loanRateAvg);
        // 累计成交额
        Double bidMoneySum = bidInfoService.queryBidMoneySum();
        model.addAttribute(IndexConstant.BID_MONEY_SUM, bidMoneySum);
        // 平台用户数
        Long userCount = userService.queryUserCount();
        model.addAttribute(IndexConstant.USER_COUNT, userCount);
        // 新手宝
        // 超过三个参数用Map存储
        HashMap<String, Object> paramsMap = new HashMap<>();
        paramsMap.put("pType", 0);
        paramsMap.put("start", 0);
        paramsMap.put("content", 1);
        List<LoanInfo> loanInfoList_X =  loanInfoService.queryLoanProduct(paramsMap);
        model.addAttribute("loanInfoList_X", loanInfoList_X);
        // 优选标
        paramsMap.put("pType", 1);
        paramsMap.put("start", 0);
        paramsMap.put("content", 4);
        List<LoanInfo> loanInfoList_Y =  loanInfoService.queryLoanProduct(paramsMap);
        model.addAttribute("loanInfoList_Y", loanInfoList_Y);
        // 散标
        paramsMap.put("pType", 2);
        paramsMap.put("start", 0);
        paramsMap.put("content", 8);
        List<LoanInfo> loanInfoList_S =  loanInfoService.queryLoanProduct(paramsMap);
        model.addAttribute("loanInfoList_S", loanInfoList_S);

        return "index";
    }

}
